package com.yunkaweilai.android.function.update;

import com.yunkaweilai.android.constants.Api;
import com.zhy.http.okhttp.OkHttpUtils;

import org.lzh.framework.updatepluginlib.business.UpdateWorker;
import org.lzh.framework.updatepluginlib.model.CheckEntity;
import org.lzh.framework.updatepluginlib.model.HttpMethod;

import okhttp3.Request;
import okhttp3.Response;

/**
 * <p>
 * 定制一个简单的使用okHttp做更新接口检查的网络任务。
 * 框架默认使用参考：{@link org.lzh.framework.updatepluginlib.business.DefaultUpdateWorker}
 * </p>
 */
public class OkhttpCheckWorker extends UpdateWorker {

    @Override
    protected String check(CheckEntity entity) throws Exception {
        // 自定网络任务。在此通过同步请求的方式调用即可
        Request.Builder builder = new Request.Builder().url(entity.getUrl());
        Response response = null;
        if (HttpMethod.GET.equals(entity.getMethod())) {
            response = OkHttpUtils
                    .get()
                    .url(entity.getUrl())
                    .build()
                    .execute();
        } else {
            response = OkHttpUtils.post().url(Api.APP_UPDATE).addParams("type","android")
                    .build().execute();

//            HttpRequest.postUrl(Api.APP_UPDATE).addParams("type", "Android").ex;
        }
        return response.body().string();
    }

}
